package kr.bos.Subclasses;

import kr.bos.Define.Key;
import kr.bos.Framework.BaseActivity;
import kr.bos.Framework.Logger;
import kr.bos.Framework.Utils.ImageLoader;
import android.R.color;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Drawable을 Drawable인 상태로 확대시키기
 * @author baek - bispro@uracle.co.kr
 * @date 2011. 12. 29.
 */
public class EnlargeDrawable extends BaseActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//배경
		LinearLayout lin = new LinearLayout(this);
		lin.setBackgroundColor(color.background_light);
		lin.setOrientation(LinearLayout.VERTICAL);

		setContentView(lin);
		
		TextView tv = new TextView(this);
		lin.addView(tv);
		
		String str = "버튼 하나라도 눌러졌으면 레나, 아니면 KLDP 이미지 긁어옵니다" + "\n";
		str+="인터넷 연결 시작하는 중";
		tv.setText(str);
		
		Drawable drawable = ImageLoader.getDrawable(mOptionA?Key.IMG_URL_LENNA:null);
		
		str = tv.getText() + "\n";
		str+="이미지 받아왓음";
		tv.setText(str);
		
		ImageView ivOrig= new ImageView(this);
		ivOrig.setBackgroundDrawable(drawable);
		ivOrig.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		lin.addView(ivOrig);
		
		str = tv.getText() + "\n";
		str+="첫번째 이미지 뿌렸음";
		tv.setText(str);
		
		Logger.e("BM NOT NULL");
		
		//drawable 사이즈 조정
		Bitmap bitmapOrg = ((BitmapDrawable) drawable).getBitmap();
		int width = bitmapOrg.getWidth();
		int height = bitmapOrg.getHeight();
		int newWidth = 170;
		int newHeight = 170;
		// calculate the scale
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// create a matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
				width, height, matrix, true);
		// make a Drawable from Bitmap to allow to set the BitMap
		drawable = new BitmapDrawable(resizedBitmap);

		str = tv.getText() + "\n";
		str+="이미지 확대 됨";
		tv.setText(str);
		
		ImageView ivEnlarged = new ImageView(this);
		ivEnlarged.setBackgroundDrawable(drawable);
		ivEnlarged.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		lin.addView(ivEnlarged);
		
		str = tv.getText() + "\n";
		str+="이미지 확대된 것 추가함";
		tv.setText(str);
		
		ImageView ivEnlarged2 = new ImageView(this);
		drawable = ImageLoader.getDrawable(mOptionA?Key.IMG_URL_LENNA:null,170,170);
		ivEnlarged2.setBackgroundDrawable(drawable);
		ivEnlarged2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		lin.addView(ivEnlarged2);
	}
}
